package com.lsfb.signln.Statistics.WithoutDepNum;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ajguan.library.EasyRefreshLayout;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 外勤部门统计
 * Created by Administrator on 2018/1/30 0030.
 */
@ContentView(R.layout.aty_without_dep_num)
public class WithoutDepNumActivity extends LsfbActivity implements DepartSignStatisticsAdapter.ItemClickListen {
    /**
     * 部门统计列表
     */
    @ViewInject(R.id.aty_without_depnum_rv)
    private RecyclerView rv_dep;
    private List<DepartSignStatisticsBean.ListBean> datas;
    private DepartSignStatisticsAdapter adapter;

    /**
     * 下拉刷新 上拉加载
     */
    @ViewInject(R.id.easylayout)
    private EasyRefreshLayout easylayout;

    private boolean IsRefresh = true;

    /**
     * 标题
     */
    @ViewInject(R.id.tv_title)
    private TextView tv_title;

    /**
     * 只看省外
     */
    @ViewInject(R.id.tv_look_out)
    private TextView tv_look_out;

    /**
     * 返回
     */
    @ViewInject(R.id.aty_back)
    private ImageView iv_back;

    //人员类型
    private String usertype = "2";
    //时间类型
    private int timetype = 0;
    //开始时间
    private String start_time = "";
    //结束时间
    private String end_time = "";
    // 内勤/外勤
    private String moldType = "";
    //页数
    private int page = 0;
    private int code;

    private MyProgressDialog dialog;

    @Override
    public void initView() {
        if (getIntent().getIntExtra("timetype", -1) != -1) {
            timetype = getIntent().getIntExtra("timetype", -1);
            if (timetype == 3) {
                start_time = getIntent().getStringExtra("startTime");
                end_time = getIntent().getStringExtra("endTime");
            }
            moldType = getIntent().getStringExtra("moldType");
            code = getIntent().getIntExtra("code", -1);
            switch (code) {
                case 0:
//                    LittleUtils.setsimpletitlebar(this, "内勤部门统计");
                    tv_title.setText("外勤部门统计");
                    tv_look_out.setVisibility(View.GONE);
                    break;
                case 1:
//                    LittleUtils.setsimpletitlebar(this, "外勤部门统计");
                    tv_look_out.setVisibility(View.VISIBLE);
                    tv_title.setText("外勤部门统计");
                    break;
                case 2:
//                    LittleUtils.setsimpletitlebar(this, "外勤补录部门统计");
                    tv_look_out.setVisibility(View.VISIBLE);
                    tv_title.setText("外勤补录部门统计");
                    break;
            }
        } else {
            T.showShort(this, "数据获取异常");
            finish();
            return;
        }

        //只看省外
        tv_look_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                moldType = "3";
                getData("3");
            }
        });

        //返回
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        dialog = new MyProgressDialog(this, "加载中......");

        datas = new ArrayList<>();
        adapter = new DepartSignStatisticsAdapter(this, datas, R.layout.item_single_crosswise, this);
        rv_dep.setLayoutManager(new LinearLayoutManager(this));
//        rv_dep.addItemDecoration(LsfbUtils.getVERTICALLineDivider(this, R.color.white_color, 10));
        rv_dep.setAdapter(adapter);

        getData(moldType);
    }

    /**
     * 获取内外勤部门人员签到数统计
     */
    private void getData(String moldType) {
        HashMap hashMap = new HashMap();
        hashMap.put("type", usertype);
        hashMap.put("timetype", String.valueOf(timetype));
        if (timetype == 3) {
            hashMap.put("starTime", start_time);
            hashMap.put("endTime", end_time);
        }
        hashMap.put("page", String.valueOf(page));
        hashMap.put("moldType", moldType);
        dialog.showdialog();
        new BaseInternet().getData(URLString.DEPARTINFO, hashMap, DepartSignStatisticsBean.class,
                new DepartSignStisticsEvent<DepartSignStatisticsBean>(), true);
    }

    @EventAnnotation
    public void eventResult(DepartSignStisticsEvent<DepartSignStatisticsBean> event) {
        dialog.hidedialog();
        easylayout.refreshComplete();
        easylayout.loadMoreComplete();
        switch (event.getCode()) {
            case 200:
                if (event.getData().getList() != null && !event.getData().getList().isEmpty()) {
                    if (IsRefresh) {
                        datas.clear();
                    }
                    datas.addAll(event.getData().getList());
                    adapter.setCount(event.getData().getCount());
                } else {
                    if (IsRefresh) {
                        datas.clear();
                    } else {
                        page--;
                        T.showShort(this, "没有更多数据");
                    }
                }
                adapter.notifyDataSetChanged();
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void Resume() {
//        getData(moldType);
    }

    /**
     * item 点击跳转
     *
     * @param model
     */
    @Override
    public void onClick(DepartSignStatisticsBean.ListBean model) {
        startActivity(new Intent(this, WithoutChildDepNumActivity.class)
                .putExtra("depid", model.getId()).putExtra("depname", model.getName())
                .putExtra("timetype", timetype)
                .putExtra("startTime", start_time).putExtra("endTime", end_time)
                .putExtra("moldType", moldType).putExtra("code", code));
    }
}
